package fpt.edu.vn.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fpt.edu.vn.dao.TopicsDao;
import fpt.edu.vn.model.RefreshTopic;

/* 
 * DetailAction.java 
 *  
 * 0.1
 * 
 * 10/15/2012 
 *  
 * Copyright notice 
 *  
 * Modification Logs: 
 *   DATE        AUTHOR      DESCRIPTION 
 *  -------------------------------------------------------- 
 *  15-Oct-2012  ThuongND    Create new 
 */
public class RefreshTopicAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private int topicId;
	private int districtId;
	private List<RefreshTopic> lstrefreshtopic;
	private RefreshTopic refreshtopic = new RefreshTopic();
	private List<Object> checkOwnerTopic;
	private List<Object> checkAdmin;
	private int count;
	private int isOwner;
	private int isAdmin;
	private boolean isRefresh = false;

	// For SessionAware
	Map<String, Object> session;

	private TopicsDao dao = new TopicsDao();

	public String execute() {
		String email = "";
		session = ActionContext.getContext().getSession();
		if (session.containsKey("emailss")) {
			email = (String) session.get("emailss");
			// get email, userid to compare with user login
			checkOwnerTopic = dao.checkOwnerTopic(email, topicId);
			checkAdmin = dao.checkAdmin(email);
			if (checkAdmin.size() < 1) {
			} else {
				setIsAdmin(1);
			}
			if (checkOwnerTopic.size() < 1) {
			} else {
				setIsOwner(1);
			}
			if (isAdmin == 1 || isOwner == 1) {
				lstrefreshtopic = dao.getrefreshTopic(topicId);
				if (lstrefreshtopic.size() < 1) {
					refreshtopic.setCountTopic(1);
					refreshtopic.setCurdate(getSysdate());
					refreshtopic.setTopicId(topicId);
					dao.addrefreshtopic(refreshtopic);
					isRefresh = dao.updateTopicRenew(topicId, getStrDate());
				} else {
					count = lstrefreshtopic.get(0).getCountTopic();
					count = count + 1;
					if (count < 11) {
						dao.updaterefreshtopic(topicId, count);
						isRefresh = dao.updateTopicRenew(topicId, getStrDate());
					}
				}

			}

		}

		return SUCCESS;

	}

	public String getStrDate() {
		String curdate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(Calendar.getInstance().getTime());
		return curdate;
	}

	public Date getSysdate() {
		Date date = new Date();
		return date;
	}

	public int getTopicId() {
		return topicId;
	}

	public void setTopicId(int topicId) {
		this.topicId = topicId;
	}

	public int getDistrictId() {
		return districtId;
	}

	public void setDistrictId(int districtId) {
		this.districtId = districtId;
	}

	public int getIsOwner() {
		return isOwner;
	}

	public void setIsOwner(int isOwner) {
		this.isOwner = isOwner;
	}

	public int getIsAdmin() {
		return isAdmin;
	}

	public void setIsAdmin(int isAdmin) {
		this.isAdmin = isAdmin;
	}

	public boolean isRefresh() {
		return isRefresh;
	}

	public void setRefresh(boolean isRefresh) {
		this.isRefresh = isRefresh;
	}

	public List<RefreshTopic> getLstrefreshtopic() {
		return lstrefreshtopic;
	}

	public void setLstrefreshtopic(List<RefreshTopic> lstrefreshtopic) {
		this.lstrefreshtopic = lstrefreshtopic;
	}

	public RefreshTopic getRefreshtopic() {
		return refreshtopic;
	}

	public void setRefreshtopic(RefreshTopic refreshtopic) {
		this.refreshtopic = refreshtopic;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

}
